package com.example.myapplication.utils

import android.util.Log
import com.example.myapplication.db.Reward
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * 不允许出现
 */
class DivisionString {
    private val gson = Gson()

    // 将 Reward 对象列表转化为 JSON 字符串存储
    fun convertToJson(rewardList: List<Reward>): String {
        return gson.toJson(rewardList)
    }

    // 从 JSON 字符串解析回 Reward 对象列表
    fun parseAwardString(awardJson: String): List<Reward>? {
        val listType = object : TypeToken<List<Reward>>() {}.type
        if(awardJson==""){
            return null
        }
        return gson.fromJson(awardJson, listType)
    }

    /**
     * 旧方法，已经抛弃
     */
    private fun cutAwardString(award: String): MutableList<MutableList<String>> {
        var state = ""
        var hasGone = ""
        var time = ""
        var price = ""
        var content = ""
        var comment = ""
        var title = ""
        var isTimeStateGone = 0
        val awardList: MutableList<MutableList<String>> = mutableListOf()

        for (i in award.indices) {
            if (i == 0) {
                if (award[i] == '0') {
                    hasGone = "0"
                } else if (award[i] == '1') {
                    hasGone = "1"
                } else {
                    Log.e("Recipe", "输入的是否完成的数据有问题: ${award[i]}")
                }
                state = "title"
            } else {
                if (award[i] == '&') {
                    awardList.add(mutableListOf(hasGone,title, time, price, content,comment))
                    hasGone = ""
                    time = ""
                    price = ""
                    content = ""
                    comment = ""
                    title=""
                    state = "hasGone"
                } else when (state) {
                    "title"-> {
                        if(award[i]=='`'){
                            state="time"
                        }else{
                            title+=award[i].toString()
                        }
                    }
                    "time" -> {
                        time += award[i].toString()
                        isTimeStateGone++
                        if (isTimeStateGone == 8) {
                            state = "price"
                            isTimeStateGone = 0
                        }
                    }
                    "hasGone" -> {
                        if (award[i] == '0') {
                            hasGone = "0"
                        } else if (award[i] == '1') {
                            hasGone = "1"
                        } else {
                            Log.e("Recipe", "输入的是否完成的数据有问题: ${award[i]}")
                        }
                        state = "time"
                    }
                    "price" -> {
                        if (award[i] == '￥') {
                            state = "content"
                            continue
                        } else {
                            price += award[i].toString()
                        }
                    }
                    "content" -> {
                        if(award[i]=='|'){
                            state="comment"
                            continue
                        }
                        content += award[i].toString()
                    }
                    "comment"->{
                        comment+=award[i].toString()
                    }
                }
            }
        }

        if (hasGone +  title+time+ price+ content + comment!="") {
            awardList.add(mutableListOf(hasGone,title, time, price, content,comment))
        }

        return awardList
    }


}
